package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import flc.ast.R;

import flc.ast.activity.CommeDetailActivity;
import flc.ast.activity.CommemorateActivity;
import flc.ast.adapter.CommemorateAdapter;

import flc.ast.bean.CommemorateBean;

import flc.ast.databinding.FragmentCommemorateBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CommemorateFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CommemorateFragment extends BaseNoModelFragment<FragmentCommemorateBinding> {

    private List<CommemorateBean> listShow = new ArrayList<>();
    private CommemorateAdapter commemorateAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_commemorate;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivAddCommemorate.setOnClickListener(this);
        mDataBinding.ivCommemorateAdd.setOnClickListener(this);
        mDataBinding.rlCommemorateBg.setOnClickListener(this);

        mDataBinding.rvCommemorateList.setLayoutManager(new LinearLayoutManager(mContext));
        commemorateAdapter = new CommemorateAdapter();
        mDataBinding.rvCommemorateList.setAdapter(commemorateAdapter);
        commemorateAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCommemorateAdd://添加记事本
            case R.id.ivAddCommemorate:
                CommemorateActivity.commemorateBean = null;
                startActivity(CommemorateActivity.class);
                break;
            case R.id.rlCommemorateBg://纪念日
                CommeDetailActivity.selPosition = 0;
                startActivity(CommeDetailActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        CommeDetailActivity.selPosition = position + 1;
        startActivity(CommeDetailActivity.class);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<CommemorateBean> listSp = AppSpUtils.getCommemorateList();
        if (listSp != null && listSp.size() > 0) {
            if (listSp.size() > 1) {
                listShow.addAll(listSp.subList(1, listSp.size()));
                commemorateAdapter.setList(listShow);
                mDataBinding.rvCommemorateList.setVisibility(View.VISIBLE);
            } else {
                mDataBinding.rvCommemorateList.setVisibility(View.GONE);
            }
            CommemorateBean bean = listSp.get(0);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            long timeSpan = TimeUtils.getTimeSpan(bean.getDate(), TimeUtils.getNowString(format), format, TimeConstants.DAY);
            if (timeSpan < 0) {//小于0表示今年这天已经过了
                timeSpan = Math.abs(timeSpan);
                mDataBinding.tvTitle.setText(getString(R.string.past_text));
            } else {
                mDataBinding.tvTitle.setText(getString(R.string.remain_text));
            }
            mDataBinding.tvTitle.setText(bean.getTitle());
            if (bean.getPath().isEmpty()) {
                mDataBinding.rlCommemorateBg.setBackgroundColor(bean.getColor());
                mDataBinding.rlCommemorateImg.setVisibility(View.GONE);
            } else {
                mDataBinding.rlCommemorateBg.setBackgroundColor(0);
                mDataBinding.rlCommemorateImg.setVisibility(View.VISIBLE);
                Glide.with(mContext).load(bean.getPath()).into(mDataBinding.ivCommemorateImg);
            }
            mDataBinding.tvDate.setText(getString(R.string.target_date) + bean.getDate());
            mDataBinding.tvDay.setText(timeSpan + "");
            mDataBinding.llCommemorate.setVisibility(View.VISIBLE);
            mDataBinding.llCommemorateNoData.setVisibility(View.GONE);
            mDataBinding.ivCommemorateAdd.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.llCommemorate.setVisibility(View.GONE);
            mDataBinding.llCommemorateNoData.setVisibility(View.VISIBLE);
            mDataBinding.ivCommemorateAdd.setVisibility(View.GONE);
        }
    }
}